<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>
<el-button type="primary" @click="dialogChapterFormVisible = true">添加章节</el-button>
<!-- 章节 -->
<ul class="chanpterList">
    <li
        v-for="chapter in chapterNestedList"
        :key="chapter.id">
        <p>
            {{ chapter.title }}

            <span class="acts">
                <el-button type="primary" @click="editVideo(chapter.id)">添加课时</el-button>
                <el-button style="" type="success" @click="editChapter(chapter.id)">编辑</el-button>
                <el-button type="danger" @click="deleteChapter(chapter.id)">删除</el-button>
            </span>
        </p>

        <!-- 视频 -->
        <ul class="chanpterList videoList">
            <li
                v-for="video in chapter.children"
                :key="video.id">
                <p>{{ video.title }}
                    <span class="acts">
                        <el-button type="success" @click="updateVideoFront(video.id)">编辑</el-button>
                        <el-button type="danger" @click="deleteVideo(video.id)">删除</el-button>
                    </span>
                </p>
            </li>
        </ul>
    </li>
</ul>
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
        <el-form :model="chapter" label-width="120px">
            <el-form-item label="章节标题">
                <el-input v-model="chapter.title"/>
            </el-form-item>
            <el-form-item label="章节排序(第几章)">
                <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item >
        <el-form-item label="是否收费">
          <el-select v-model="video.isFree" clearable placeholder="是否收费">
            <el-option :value="0" label="收费"/>
            <el-option :value="1" label="免费"/>
          </el-select>
        </el-form-item>


        <el-form-item label="上传视频">
            <el-upload
                   :on-success="handleVodUploadSuccess"
                   :on-remove="handleVodRemove"
                   :before-remove="beforeVodRemove"
                   :on-exceed="handleUploadExceed"
                   :file-list="fileList"
                   :action="'http://localhost:8102/eduvod/video/uploadAliyunVideo'"
                   :limit="1"
                   class="upload-demo">
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
                <div slot="content">最大支持1G，<br>
                    支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                    GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                    MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                    SWF、TS、VOB、WMV、WEBM 等视频格式上传</div>
                <i class="el-icon-question"/>
            </el-tooltip>
            </el-upload>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
</ul>
    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="save">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import chapter from'@/api/chapter/chapter'
import video from'@/api/video/video'
export default {
  data() {
    return {
      courseId: '', // 所属课程
      chapterNestedList: [] ,// 章节嵌套课时列表
      saveBtnDisabled:false,
      saveVideoBtnDisabled:false,
      dialogChapterFormVisible: false, //是否显示章节表单
      chapter: {// 章节对象
        courseId:'',
        title: '',
        sort: 0
      },

      dialogVideoFormVisible: false, // 是否显示课时表单
      chapterId: '', // 课时所在的章节id
      video: {// 课时对象
        courseId:'',
        chapterId:'',
        title: '',
        sort: 0,
        isFree: 0,
        videoSourceId: '',
        videoOriginalName:'',
      },
      fileList:[],
      loading:false
    }
  },

  created() {
    this.init()
  },
//=========================================课程操作及页面===========================================
  methods: {
  init() {
    if (this.$route.params && this.$route.params.id) {
      this.courseId = this.$route.params.id
      // 根据id获取课程基本信息
      this.getChapterAndVideoByCourseId()
      console.log(this.chapterNestedList)
    }
  },
  //通过课程id查询章节和小结
  getChapterAndVideoByCourseId() {
    chapter.getChapterAndVideoByCourseId(this.courseId).then(response => {
      this.chapterNestedList = response.data.list
    })
  },
  //上一步
  previous(){
    this.$router.push({ path: '/course/info/'+this.courseId })
  },
  //下一步
  save(){
     this.$router.push({ path: '/course/publish/'+this.courseId })
  },
  //=======================================章节操作================================================
  helpSave(){
    this.dialogChapterFormVisible = false// 如果保存成功则关闭对话框
    this.chapter.title = ''// 重置章节标题
    this.chapter.sort = 0// 重置章节标题
    this.saveBtnDisabled = false
    this.getChapterAndVideoByCourseId()//刷新列表
  },
  //判断是添加操作还是修改操作
  saveOrUpdate(){
    if(!this.chapter.id){
      this.addChapter()
    }else{
      this.updateChapter()
    }
  },
  //添加章节
  addChapter(){
    //将课程id设置到封装到章节对象中
    this.chapter.courseId=this.courseId
    chapter.addChapter(this.chapter)
    .then(response =>{
      //提示信息
       this.$message({
        type: 'success',
        message: '添加成功!'
      });
      //跳转到章节列表
       this.helpSave()
    })
  },
  //弹出修改章节框，并根据章节id加载信息
  editChapter(chapterId){
       this.dialogChapterFormVisible = true
       chapter.getChapterById(chapterId).then(response => {
           this.chapter = response.data.chapter
       })
  },
  //修改章节
  updateChapter(){
    chapter.updateChapter(this.chapter)
    .then(response =>{
      //提示信息
       this.$message({
        type: 'success',
        message: '修改成功!'
      });
      //跳转到章节列表
       this.helpSave()
    })
  },

    //删除章节
    deleteChapter(chapterId) {
    this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
       chapter.deleteChapter(chapterId)
       .then(response=>{
         this.$message({
           type: 'success',
           message: '删除成功!'
         })
         location.reload()//刷新页面
       }).catch(response=>{
         this.$message({
           type: 'error',
           message: response.message
         })
       })
    })
  },
  //========================================课时操作=============================
  helpSaveVideo() {
    this.dialogVideoFormVisible = false// 如果保存成功则关闭对话框
    this.getChapterAndVideoByCourseId()//重新加载页面
    this.video.title = ''// 重置章节标题
    this.video.sort = 0// 重置章节标题
    this.video.videoSourceId = ''// 重置视频资源id
    this.saveVideoBtnDisabled = false
  },
  //添加课时或修改课时保存操作
  saveOrUpdateVideo(){
    if(this.video.id){
      this.updateVideo()
      this.fileList=[]
    }else{
      this.addVideo()
      this.fileList=[]
    }
  },
  //上传视频成功调用的方法
  handleVodUploadSuccess(response,file,fileList){
    this.video.videoSourceId=response.data.videoId
    this.video.videoOriginalName=response.data.videoOriginalName
    this.$message({
      type: 'success',
      message: '上传成功'
    })

  },
  handleUploadExceed(){
    this.$message.warning('想要重新上传视频,请先删除已经上传的视频')
  },
  //删除视频之前
  beforeVodRemove(file,fileList){

  },
  //删除视频后端接口调用
  handleVodRemove(){
    video.deleteAliyunVideo(this.video.videoSourceId)
    .then(response=>{
      this.$message({
        type: 'success',
        message: '删除视频成功'
      })
      this.fileList=[]//视频列表清空
      this.video.videoSourceId=''
      this.video.videoOriginalName=''
    })
  },




  //添加课时操作前端
  editVideo(chapterId){
    this.video={}
    this.dialogVideoFormVisible=true
    this.video.courseId=this.courseId
    this.video.chapterId=chapterId
    this.fileList=[]
  },
  //添加课时操作后端数据处理
  addVideo(){
    video.addVideo(this.video)
    .then(response =>{
      //提示信息
       this.$message({
        type: 'success',
        message: '添加成功!'
      });
      //跳转到章节列表
       this.helpSaveVideo()
    })
  },
  //删除课时操作
  deleteVideo(chapterId){
    this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
       video.deleteVideo(chapterId)
       .then(response=>{
         this.$message({
           type: 'success',
           message: '删除成功!'
         })
         location.reload()//刷新页面
       }).catch(response=>{
         this.$message({
           type: 'error',
           message: response.message
         })
       })
    })
  },
  //修改课时后端数据返回操作
  updateVideo(){
    video.updateVideo(this.video)
   .then(response =>{
     //提示信息
      this.$message({
       type: 'success',
       message: '修改成功!',
     });
     //跳转到章节列表
      this.helpSaveVideo()
   })
  },
  //修改课时前端操作
  updateVideoFront(videoId){
    this.video.id=videoId
    //根据课时Id查询课时
    video.selectVideoById(videoId)
    .then(response=>{
       this.video=response.data.video
       if(response.data.video.videoSourceId){
         this.fileList = [{'name': this.video.videoOriginalName}]
       }else{
         this.fileList =[]
       }

    })
    this.dialogVideoFormVisible=true
  }
  }
}
</script>

<style scoped>
.chanpterList p{
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chanpterList .acts {
    float: right;
}
</style>
